/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package embeddedpaint.model.layers;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;

public class LayerText extends ALayer
{
    private static final String M_DEFAULT_TEXT = "";
    
    private String              m_text;
    private Color               m_color;
    private Font                m_font;
    private int                 m_ascent;

    public LayerText (String m_name)
    {
        super(m_name);
        
        this.m_text = M_DEFAULT_TEXT;
    }
    
    public LayerText ()
    {
        this("");
    }

    private void updateSize ()
    {
        Image       img = new BufferedImage(1, 1, BufferedImage.BITMASK);
        Graphics    g   = img.getGraphics();
        FontMetrics fm  = g.getFontMetrics(m_font);

        this.m_height   = fm.getHeight();
        this.m_width    = fm.stringWidth(this.m_text);
        this.m_ascent   = fm.getAscent();

        this.updateObservers();
    }

    @Override
    public void drawLayer (Graphics2D g2d)
    {
        g2d.setFont   (m_font);
        g2d.setColor  (m_color);
        g2d.drawString(this.m_text, m_x, m_y + m_ascent);
    }

    public String getM_text ()
    {
        return m_text;
    }

    public void setM_text (String m_text)
    {
        this.m_text = m_text;
        this.updateSize();
    }

    public Color getM_color ()
    {
        return m_color;
    }

    public void setM_color (Color m_color)
    {
        this.m_color = m_color;
        this.updateObservers();
    }

    public Font getM_font ()
    {
        return m_font;
    }

    public void setM_font (Font m_font)
    {
        this.m_font = m_font;
        this.updateSize();
    }

    public ALayer getLayerImage ()
    {
        LayerImage image = new LayerImage(m_name);

        image.setM_img   (this.getIcon(m_width, m_height));
        image.setM_height(m_height);
        image.setM_width (m_width);
        image.setM_x     (m_x);
        image.setM_y     (m_y);

        return image;
    }
}
